package imper.main.view;

import imper.engine.entity.Country;
import imper.engine.entity.Resources;
import imper.engine.entity.ResourcesType;
import imper.engine.util.ParameterManager;
import imper.engine.util.ResourceManager;
import imper.graphic.AppliState;
import imper.graphic.ButtonImage;
import imper.main.BarMenuUtil;
import imper.main.ImageManager;
import imper.main.listener.AddTransportRealListener;
import imper.main.listener.RemoveTransportRealListener;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class TransportView extends AppliState
{

	public TransportView()
	{
		super(EnumView.TRANSPORTVIEW.getId());
	}

	@Override
	public void init(GameContainer pGc, StateBasedGame pSbg) throws SlickException
	{
		BarMenuUtil.init(pGc, pSbg, this);
		Image droite = new Image("data/pictuniv_9903.bmp");
		Image gauche = new Image("data/pictuniv_9901.bmp");
		int nbRes = 0;
		for (ResourcesType resourceType : ParameterManager.getInstance().getResourcesType().values())
		{
			Image image = ImageManager.getInstance().getImage(ImageManager.RESOURCE, resourceType.getId());
			if (!Boolean.TRUE.equals(resourceType.isWorkForce()) && image != null)
			{
				int calcY = 50 + nbRes / 3 * 50;
				int calcX = 100 + nbRes % 3 * 200;
				ButtonImage button = new ButtonImage(calcX + 40, calcY + 10, gauche);
				button.addListener(new RemoveTransportRealListener(resourceType));
				addElement(button);
				ButtonImage button1 = new ButtonImage(calcX + 70, calcY + 10, droite);
				button1.addListener(new AddTransportRealListener(resourceType));
				addElement(button1);
				nbRes++;
			}
		}
	}

	@Override
	public void render(GameContainer pGameContainer, StateBasedGame pSbg, Graphics pGraphics) throws SlickException
	{
		Country country = ParameterManager.getInstance().getCountry();
		int nbRes = 0;
		ResourceManager transportCalc = country.getResourceTransportCalc();
		ResourceManager transportReal = country.getResourceTransportReal();
		for (ResourcesType resourceType : ParameterManager.getInstance().getResourcesType().values())
		{
			// Resources resource =
			// managerStock.getResources(resourceType.getId());
			Image image = ImageManager.getInstance().getImage(ImageManager.RESOURCE, resourceType.getId());
			if (!Boolean.TRUE.equals(resourceType.isWorkForce()) && image != null)
			{
				int calcY = 50 + nbRes / 3 * 50;
				int calcX = 100 + nbRes % 3 * 200;
				pGraphics.drawImage(image, calcX, calcY);
				// ecriture des valeurs
				Resources resourceCalc = transportCalc.getResources(resourceType.getId());
				Resources resourceReal = transportReal.getResources(resourceType.getId());
				pGraphics.drawString(resourceReal.getValue() + "/" + resourceCalc.getValue(), calcX + 100, calcY + 10);
				nbRes++;
			}
		}
		super.draw(pGameContainer, pGraphics);

	}

	@Override
	public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException
	{
		// TODO Auto-generated method stub

	}

}
